<template>
  <div class="post-detail-container">
    <!-- 作者信息区 -->
    <div class="author-section">
      <div class="author-info">
        <el-image
          loading="lazy"
          :src="postInfo?.avatar"
          class="author-avatar"
          fit="cover"
        >
          <template #error>
            <el-image
              loading="lazy"
              :src="errorImageUrlConstant"
              class="author-avatar"
              fit="cover"
            />
          </template>
        </el-image>
        <div class="author-meta">
          <span class="author-name">{{ postInfo?.username }}</span>
          <span class="post-time" v-if="postInfo?.createTime">
            {{ postInfo.createTime }}
          </span>
        </div>
      </div>
      <div class="action-buttons" v-if="isAdmin">
        <el-button type="warning" size="small" @click="handleEdit">
          <el-icon><Edit /></el-icon> 编辑
        </el-button>
        <el-button type="danger" size="small" @click="handleDelete">
          <el-icon><Delete /></el-icon> 删除
        </el-button>
      </div>
    </div>

    <!-- 帖子内容区 -->
    <div class="post-content-section">
      <h1 class="post-title">
        {{ postInfo?.title }}
      </h1>

      <div class="post-meta">
        <span class="meta-item">
          <el-icon><View /></el-icon>
          {{ postInfo?.viewCount || 0 }} 浏览
        </span>
        <span class="meta-item">
          <el-icon><ChatDotRound /></el-icon>
          {{ postInfo?.commentCount || 0 }} 评论
        </span>
        <span class="meta-item">
          <el-icon><Star /></el-icon>
          {{ postInfo?.likes || 0 }} 点赞
        </span>
      </div>

      <el-divider />

      <div class="post-content" v-html="postInfo?.content"></div>

      <div class="post-tags" v-if="postInfo?.tags?.length">
        <el-tag
          v-for="tag in postInfo.tags"
          :key="tag"
          size="small"
          class="tag-item"
        >
          {{ tag }}
        </el-tag>
      </div>
    </div>

    <!-- 评论区 -->
    <div class="comment-section">
      <h3 class="comment-title">
        <el-icon><ChatDotRound /></el-icon>
        评论 ({{ postInfo?.commentCount || 0 }})
      </h3>

      <!-- 评论列表 -->
      <div class="comment-list">
        <div class="comment-item" v-for="comment in comments" :key="comment.id">
          <el-avatar :size="40" :src="comment.avatar" class="comment-avatar">
            <span v-if="!comment.avatar">{{ comment.username.charAt(0) }}</span>
          </el-avatar>

          <div class="comment-content">
            <div class="comment-header">
              <span class="user-name">{{ comment.username }}</span>
              <span class="comment-time">{{ comment.commentTime }}</span>
            </div>

            <div class="comment-text">{{ comment.commentContent }}</div>
          </div>
        </div>

        <!-- 无评论提示 -->
        <div class="empty-comments" v-if="comments.length === 0">
          <el-empty description="暂无评论" :image-size="80" />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
import DOMPurify from "dompurify";
import {
  errorImageUrlConstant,
  /* formatDate  */
} from "@/constant";
import { getPostCommentListApi } from "@/apis/comment";
import { getPostApi } from "@/apis/post";
import { ElMessage, ElLoading } from "element-plus";

const route = useRoute();
const postInfo = ref();
const comments: any = ref([]);
const isAdmin = ref(true); // 根据实际权限设置
// const showComments = ref(true); // 可配置是否显示评论

async function getPost() {
  const loading = ElLoading.service({
    lock: true,
    text: "Loading",
    background: "rgba(0, 0, 0, 0.7)",
  });
  try {
    const res: any = await getPostApi(route.params.id);
    if (res.code === 0) {
      res.data.content = decodeHtml(res.data.content);
      postInfo.value = res.data;

      await handleShowComments();
    } else {
      ElMessage({
        type: "error",
        message: "获取帖子详情失败",
      });
    }
  } catch (error) {
    ElMessage({
      type: "error",
      message: "获取帖子详情失败",
    });
  } finally {
    loading.close();
  }
}

function decodeHtml(html: any) {
  return DOMPurify.sanitize(html);
}

function handleEdit() {
  // 编辑帖子逻辑
  console.log("编辑帖子:", postInfo.value?.id);
}

function handleDelete() {
  // 删除帖子逻辑
  console.log("删除帖子:", postInfo.value?.id);
}

async function handleShowComments() {
  try {
    const res: any = await getPostCommentListApi(
      { page: 1, size: 10 },
      postInfo.value?.id
    );
    comments.value = res.data.records;
  } catch (error) {}
}

onMounted(() => {
  getPost();
});
</script>

<style scoped>
/* 帖子详情页主题适配样式 */
.post-detail-container {
  max-width: 100%;
  margin: 0 auto;
  padding: 20px;
  background-color: var(--el-bg-color);
  box-shadow: var(--el-box-shadow-light);
  border-radius: var(--el-border-radius-base);
  color: var(--el-text-color-regular);
  border: 1px solid var(--el-border-color);
  transition: all 0.3s var(--el-transition-function-ease-in-out-bezier);
}

/* 作者信息区域 */
.author-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 30px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--el-border-color);
}

.author-info {
  display: flex;
  align-items: center;
}

.author-avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  margin-right: 15px;
  border: 1px solid var(--el-border-color);
  background-color: var(--el-fill-color-light);
  overflow: hidden;
}

.author-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.author-meta {
  display: flex;
  flex-direction: column;
}

.author-name {
  font-size: 18px;
  font-weight: 500;
  color: var(--el-text-color-primary);
}

.post-time {
  font-size: 14px;
  color: var(--el-text-color-secondary);
  margin-top: 5px;
}

/* 帖子内容区域 */
.post-content-section {
  padding: 0 10px;
}

.post-title {
  font-size: 28px;
  font-weight: 600;
  color: var(--el-text-color-primary);
  margin-bottom: 20px;
  line-height: 1.4;
  position: relative;
}

.post-meta {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  flex-wrap: wrap;
  gap: 15px;
}

.post-meta .meta-item {
  display: flex;
  align-items: center;
}

.post-meta .meta-item .el-icon {
  margin-right: 5px;
  color: var(--el-text-color-secondary);
}

.post-content {
  line-height: 1.8;
  font-size: 16px;
  color: var(--el-text-color-regular);
}

.post-content :deep(p) {
  margin-bottom: 1.5em;
  word-break: break-word;
}

.post-content :deep(img) {
  max-width: 100%;
  height: auto;
  border-radius: var(--el-border-radius-base);
  margin: 10px 0;
  opacity: 1;
  transition: opacity 0.3s;
}

[data-theme="dark"] .post-content :deep(img) {
  opacity: 0.9;
}

[data-theme="dark"] .post-content :deep(img:hover) {
  opacity: 1;
}

.post-content :deep(a) {
  color: var(--el-color-primary);
  text-decoration: none;
}

.post-content :deep(a:hover) {
  color: var(--el-color-primary-light-3);
  text-decoration: underline;
}

.post-content :deep(pre) {
  background-color: var(--el-fill-color-light);
  padding: 12px;
  border-radius: var(--el-border-radius-base);
  overflow-x: auto;
}

.post-content :deep(code) {
  font-family: var(--el-font-family-mono);
  background-color: var(--el-fill-color-light);
  padding: 2px 4px;
  border-radius: 3px;
  color: var(--el-color-danger);
}

/* 标签区域 */
.post-tags {
  margin-top: 30px;
  padding-top: 20px;
  border-top: 1px dashed var(--el-border-color);
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.tag-item {
  margin-right: 10px;
  margin-bottom: 10px;
}

/* 评论区 */
.comment-section {
  margin-top: 40px;
  padding-top: 30px;
  border-top: 1px solid var(--el-border-color);
}

.comment-title {
  font-size: 20px;
  color: var(--el-text-color-primary);
  margin-bottom: 20px;
  display: flex;
  align-items: center;
}

.comment-title .el-icon {
  margin-right: 8px;
  font-size: 22px;
  color: var(--el-color-primary);
}

.comment-list .comment-item {
  display: flex;
  padding: 15px 0;
  border-bottom: 1px solid var(--el-border-color-light);
}

.comment-list .comment-item:last-child {
  border-bottom: none;
}

.comment-list .comment-item .comment-avatar {
  margin-right: 15px;
  flex-shrink: 0;
  width: 40px;
  height: 40px;
}

.comment-list .comment-item .comment-content {
  flex-grow: 1;
}

.comment-list .comment-item .comment-content .comment-header {
  display: flex;
  align-items: center;
  margin-bottom: 8px;
  font-size: 14px;
}

.comment-list .comment-item .comment-content .comment-header .user-name {
  font-weight: 500;
  color: var(--el-text-color-primary);
  margin-right: 10px;
}

.comment-list .comment-item .comment-content .comment-header .comment-time {
  color: var(--el-text-color-placeholder);
  font-size: 12px;
}

.comment-list .comment-item .comment-content .comment-text {
  line-height: 1.6;
  color: var(--el-text-color-regular);
  white-space: pre-wrap;
  word-break: break-word;
}

.comment-list .empty-comments {
  padding: 30px 0;
  text-align: center;
  color: var(--el-text-color-secondary);
}

/* 响应式设计 */
@media (max-width: 992px) {
  .post-title {
    font-size: 24px;
  }

  .author-section {
    flex-direction: column;
    align-items: flex-start;
    gap: 15px;
  }
}

@media (max-width: 768px) {
  .post-detail-container {
    padding: 15px;
    border-radius: 0;
    border-left: none;
    border-right: none;
  }

  .post-title {
    font-size: 22px;
  }

  .post-meta {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
  }

  .comment-item {
    flex-direction: column;
  }

  .comment-item .comment-avatar {
    margin-right: 0;
    margin-bottom: 10px;
  }
}

@media (max-width: 480px) {
  .post-content {
    font-size: 15px;
  }

  .post-content :deep(img) {
    border-radius: 0;
    margin-left: -15px;
    margin-right: -15px;
    max-width: calc(100% + 30px);
  }
}
</style>
